package com.xixiake.activity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.xixiake.R;
import com.xixiake.application.BaseApplication;

/**
 * Created by c-wanghengli on 2017/4/4.
 */
public class MainFragment extends FrameFragment {
    View mRootView;
    View[] mTabs;

    public static MainFragment getInstance(){
        MainFragment mInstance = new MainFragment();
        return mInstance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(mRootView == null){
            mRootView = super.onCreateView(inflater,container,savedInstanceState);
        }else{
            if(mRootView.getParent() != null){
                ((ViewGroup)mRootView.getParent()).removeView(mRootView);
            }
        }
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        if(mTabs == null){
            initTabs();
            setFrames();
            setCurrentTab(0);
        }
        super.onViewCreated(view, savedInstanceState);
    }




    private void initTabs() {
        View tabTrip = createTabItem(R.drawable.tab_trip_selector,R.string.tab_trip);
        View tabRoute = createTabItem(R.drawable.sel_tab_route,R.string.tab_route);
        View tabMessage = createTabItem(R.drawable.sel_message,R.string.tab_discovery);
        View tabMine = createTabItem(R.drawable.tab_own_selector,R.string.tab_own);
        mTabs = new View[]{
          tabTrip,tabRoute,tabMessage,tabMine
        };
    }

    private View createTabItem(int tab_trip_selector, int tab_trip) {
        View tabItem = View.inflate(BaseApplication.getContext(),R.layout.main_tab_item,null);
        ImageView imageView = (ImageView) tabItem.findViewById(R.id.tab_item_img);
        imageView.setBackgroundResource(tab_trip_selector);
        TextView textView = (TextView) tabItem.findViewById(R.id.tab_item_txt);
        textView.setText(tab_trip);
        return tabItem;
    }

    private void setFrames() {
        addFrame(mRootView,HomePageFrame.class,mTabs[0]);
        addFrame(mRootView,RoutePageFrame.class,mTabs[1]);
        addFrame(mRootView,ImMessageFrame.class,mTabs[2]);
        addFrame(mRootView,OwnFrame.class,mTabs[3]);
    }
}
